package csel.controller.decoders;

import csel.controller.dispatchers.AbstractDispatcher;
import csel.controller.factories.CommandFactory;
import csel.controller.factories.ModelCommandFactory;
import csel.controller.factories.ViewCommandFactory;
import csel.controller.listeners.SplashViewActionListener;
import csel.model.GameCommand;

/**
 * Reasons for Existence: Decodes actions in the splash 
 * screen / game setup view, selects a dispatcher and command factory</br>
 * Team Members Responsible</br>
 * Implementation: Nik, Tait</br>
 * Testing: Nik, Tait</br> 
 * 
 * @author Nikolas Wolfe
 */
public class SplashActionEventDecoder extends AbstractActionEventDecoder
{
	private boolean view;
	private boolean model;
	private GameCommand viewGameCommand;
	private GameCommand modelGameCommand;
	// implement constructor
	public SplashActionEventDecoder(SplashViewActionListener listener,
			AbstractDispatcher modelDispatcher,
			AbstractDispatcher viewDispatcher,
			ModelCommandFactory modelCommandFactory,
			ViewCommandFactory viewCommandFactory)
	{
		this.listener = listener;
		this.modelDispatcher = modelDispatcher;
		this.viewDispatcher = viewDispatcher;
		this.modelCommandFactory = modelCommandFactory;
		this.viewCommandFactory = viewCommandFactory;
		view = false;
		model = false;
	}
	
	//overrides superclass and determines what command should be created and sets the creation in motion
	@Override
	public void parseCommand() throws InterruptedException
	{
		view = false;
		model = false;
		String cmd = actionEvent.getActionCommand();
		if (cmd == "spquit")
		{
			System.out.println("[CONTROLLER] The quit command was parsed");
			this.setToModel();
			modelCommandFactory.newQuitCommandFactory();
			model = true;
		} else if (cmd == "spnew")
		{
			System.out.println("[CONTROLLER] The new command was parsed");
			this.setToModel();
			modelCommandFactory.newNewCommandFactory();
			model = true;
			this.setToView();
			viewCommandFactory.newChangeToGameViewCommandFactory();
			view = true;
		} else if (cmd == "spload")
		{
			System.out.println("[CONTROLLER] The load command was parsed");
			this.setToModel();
			modelCommandFactory.newLoadCommandFactory();
			model = true;
			this.setToView();
			viewCommandFactory.newChangeToGameViewCommandFactory();
			view = true;
		} else
		{
			System.out
					.println("[CONTROLLER] " + cmd + "is not a valid command");
		}
	}

	@Override
	public void createCommand()
	{
		if (model)
		{
			this.modelGameCommand = this.modelCommandFactory
					.createGameCommand();
		}
		if (view)
		{
			this.viewGameCommand = this.viewCommandFactory.createGameCommand();
		}

	}

	@Override
	public void sendCommand() throws InterruptedException
	{
		if (model)
		{
			this.modelDispatcher.export(this.modelGameCommand);
		}
		if (view)
		{
			this.viewDispatcher.export(this.viewGameCommand);
		}
	}
}